﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TFSTools.TFSLibrary.TestManagement;

namespace TFSTools.TestManagement.TestCaseReview
{
    /// <summary>
    /// Interaction logic for TestCaseView
    /// </summary>
    public partial class TestCaseView : Window
    {
        #region DataContext

        private TestCaseDataContext viewDataContext;

        public TestCaseDataContext ViewDataContext
        {
            get { return this.viewDataContext; }
            set
            {
                this.viewDataContext = value;
                this.DataContext = value;
            }
        }

        public new object DataContext
        {
            get { return base.DataContext; }
            private set
            {
                base.DataContext = value;
            }
        }

        #endregion
        
        #region Constructor
        
        public TestCaseView()
        {
            this.InitializeComponent();
        }
        
        #endregion

        #region Window Events
        
        private void TestCaseStepsItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            e.RemovedItems.OfType<TestActionViewItem>().ToList().ForEach(x => x.IsSelected = false);
            e.AddedItems.OfType<TestActionViewItem>().ToList().ForEach(x => x.IsSelected = true);
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBoxResult.Yes;

            if (this.ViewDataContext.IsCommentsDirty)
            {
                result = MessageBox.Show("Are you sure you want to Refresh?\n\nRefreshing this Test Case will cause all\nof your comments to be lost.", "Refresh", MessageBoxButton.YesNo, MessageBoxImage.Question);
            }

            if (result == MessageBoxResult.Yes)
            {
                ////var selectedIndex = this.ViewDataContext.TestCase.IterationData.IndexOf(this.ViewDataContext.TestCase.SelectedIteration);

                this.ViewDataContext.Refresh();

                ////this.ViewDataContext.SelectedIteration = this.ViewDataContext.TestCase.IterationData[selectedIndex];
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            this.ViewDataContext.Save();
        }

        private void InsertCommentButton_Click(object sender, RoutedEventArgs e)
        {
            var stepWarningDialog = new StepCommentWarning();
            stepWarningDialog.Owner = this;
            stepWarningDialog.ShowWarning(this.ViewDataContext.TestCase);

            this.ViewDataContext.InsertComment();
        }

        private void ClearAllCommentsButton_Click(object sender, RoutedEventArgs e)
        {
            this.ViewDataContext.ClearAllComments();
        }

        private void PreviousIterationButton_Click(object sender, RoutedEventArgs e)
        {
            this.ViewDataContext.PreviousIteration();
        }

        private void NextIterationButton_Click(object sender, RoutedEventArgs e)
        {
            this.ViewDataContext.NextIteration();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult result = MessageBoxResult.No;

            if (this.ViewDataContext.IsTestCaseDirty)
            {
                result = MessageBox.Show("Changes have been detected for this test case.\n\nDo you want to save these changes?", "Refresh", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
            }

            if (result == MessageBoxResult.Cancel)
            {
                e.Cancel = true;
            }
            else if (result == MessageBoxResult.Yes)
            {
                this.ViewDataContext.Save();
            }
        }

        #endregion
    }
}
